package cs575.common;

import java.nio.ByteBuffer;

public class FantasyMessage {
	private static final int HEADER_LENGTH = 1;
	
	private byte opCode = OpCodes.EMPTY;
	private byte[] message;
	
	/**
	 * @return Length of the byte array that represents this Poster message.
	 */
	private int getMessageLength() {		
		//Magic number: 4 is the size of an integer in bytes.
		return HEADER_LENGTH + 4 + message.length;
	}
	
	/**
	 * Converts this Poster message into a byte array that will be sent over the network.
	 * @return Byte array representing this Poster message.
	 * @throws MessageFormatException - If the data contained within this Poster message is invalid.
	 */
	public byte[] toBytes() {
		ByteBuffer bb = ByteBuffer.allocate(getMessageLength());
		
		bb.put(opCode);
		bb.putInt(message.length);
		bb.put(message);
		
		return bb.array();
	}
	
	public String toString() {
		StringBuilder b = new StringBuilder();
		
		b.append("Op Code: " + opCode + "\n");
		b.append("Message: " + new String(message));
		
		return b.toString();
	}
	
	public void setOpCode(byte opCode) {
		this.opCode = opCode;
	}
	public byte getOpCode() {
		return opCode;
	}

	public byte[] getMessage() {
		return message;
	}

	public void setMessage(byte[] message) {
		this.message = message;
	}
	
}